<?php

require_once 'common/conn.php';
require_once 'share.action.php';

$result = array();
if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}
if (isset($_GET['code'])) {
    $code = $_GET['code'];
}

if (isset($_GET['type'])) {
    $type = $_GET['type'];
}

$db = new DBC();
$result['errorNo'] = 0;
if (isset($type) && isset($usercode) && isset($code)) {
    if ($type == "music") {
        $type = 4;
        $sql_product = $sql = "SELECT name FROM t_music WHERE flashcode = '" . $code . "'";
        $msg = "我购买了音乐《";
    } else {
        $type = 1;
        $sql_product = $sql = "SELECT name FROM t_animation WHERE flashcode = '" . $code . "'";
        $msg = "我购买了动画《";
    }
    $sql = "DELETE FROM t_device_product WHERE usercode = '" . $usercode . "' AND flashcode = '" . $code . "' AND type = " . $type;
    $db->execute($sql);
    $sql = "INSERT INTO t_device_product(usercode,flashcode,downloadTime,type,createTime) VALUES('"
            . $usercode . "','" . $code . "',now()," . $type . ",now())";
    if ($db->execute($sql)) {
        $share = array();
        $share['usercode'] = $usercode;
        $share['type'] = 1;
        $data = $db->fetchData($sql_product);
        if ($data != false && count($data) > 0) {
            $share['message'] = $msg . $data[0]['name'] . "》。";
            updateShare($share);
        }
        $result['errorNo'] = "0";
        $result['errorMsg'] = "";
    } else {
        $result['errorNo'] = "1";
        $result['errorMsg'] = "执行错误";
    }
} else {
    $result['errorNo'] = 1;
    $result['errorMsg'] = "提交参数不完整";
}
echo json_encode($result);
?>
